<template>
  <div class="cate-lists">
    <!-- searchBar -->
    <search-bar @onSearch="onSearch"/>
    <!-- /searchBar -->
    <el-button type="primary" @click="cateAddShow = true">新增商品分类</el-button>
    <el-table :data="cates" style="width: 100%;margin-top:15px;height: 500px" >
      <el-table-column prop="cateName" label="分类名称"  />
      <el-table-column prop="icon" label="分类图标">
        <template #default="{row}">
          <img :src="row.icon" width="80" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="是否启用">
        <template #default="{row}">
          <el-switch
            v-model="row.enable"
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="swicthEnable(row.id, row.enable)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{row}">
          <el-button type="success" @click="showUpdateDialog(row.id)" size="small">编辑</el-button>
          <el-button type="danger" @click="delItem(row.id)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      layout="prev, pager, next, sizes, jumper, total"
      v-model:current-page="page"
      v-model:page-size="pageSize"
      background :total="total"
      @size-change="sizePage"
      @current-change="pageChange"
      />
    <!-- 增加商品分类弹窗 -->
    <el-dialog v-model="cateAddShow" title="增加商品分类">
      <cate-add/>
    </el-dialog>
    <!-- /增加商品分类弹窗 -->
    <!-- 修改商品分类弹窗 -->
    <el-dialog v-model="cateUpdateShow" title="修改商品分类">
      <cate-update :id="cateId"/>
    </el-dialog>
    <!-- /修改商品分类弹窗 -->
  </div>
</template>

<script>
import { fetchCateLists, updateCate, delCate } from '@a/cateLists'
import SearchBar from '@/components/SearchBar.vue'
import CateAdd from './components/CateAdd.vue'
import CateUpdate from './components/CateUpdate.vue'
export default {
  name: 'CateLists',
  data () {
    return {
      cates: [],
      // 商品列表搜索的参数
      total: 0,
      page: 1,
      pageSize: 10,
      searchParams: {
        keyWords: '',
        rangeDate: []
      },
      cateAddShow: false,
      cateUpdateShow: false,
      cateId: -1
    }
  },
  methods: {
    delItem (id) {
      // 删除商品
      // 弹出确认框，确认后删除
      this.$confirm('此操作会彻底删除分类,确定继续吗?', '删除分类').then(() => {
        delCate({ id }).then(res => {
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: res.data.msg,
              duration: 1500,
              onClose: () => {
                // 刷新页面
                this.$router.go(0)
              }
            })
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg
            })
          }
        })
      })
    },
    showUpdateDialog (id) {
      // 点击 编辑 显示修改商品弹窗
      this.cateUpdateShow = true
      this.cateId = id
    },
    pageChange () {
      this.fetchCateLists()
    },
    sizePage () {
      this.fetchCateLists()
    },
    onSearch (searchParams) {
      // 点击顶部搜索框，开始搜索
      this.searchParams = searchParams
      this.fetchCateLists()
    },
    // 点击开关修改当前分类 启用状态
    swicthEnable (id, enable) {
      console.log(id, enable)
      // 行内修改商品分类启动状态
      updateCate({
        id,
        enable
      }).then(res => {
        if (res.data.code === 200) {
          // 成功后 弹出消息提示框提醒用户
          this.$message({
            type: 'success',
            message: res.data.msg,
            duration: 2000,
            onClose: () => {
              // 刷新页面
              this.$router.go(0)
            }
          })
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg
          })
        }
      })
    },
    fetchCateLists () {
      fetchCateLists({
        keyWords: this.searchParams.keyWords,
        startTime: this.searchParams.rangeDate[0],
        endTime: this.searchParams.rangeDate[1],
        page: this.page,
        pageSize: this.pageSize
      }).then(res => {
        if (res.data.code === 200) {
          this.cates = res.data.data.lists
          this.total = res.data.data.total
        }
      })
    }
  },
  created () {
    this.fetchCateLists()
  },
  components: {
    SearchBar,
    CateAdd,
    CateUpdate
  }
}
</script>

<style lang="scss" scoped>

</style>
